package com.ws.service.impl;

import com.github.pagehelper.Page;
import com.ws.common.CommonConstant;
import com.ws.common.PaasPage;
import com.ws.entity.Book;
import com.ws.entity.LoginUser;
import com.ws.repository.LoginUserRepository;
import com.ws.service.LoginUserService;
import com.ws.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author ：ws
 * @date ：Created in 2023/11/15 15:54
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class LoginUserServiceImpl implements LoginUserService {
    @Autowired
    LoginUserRepository loginUserRepository;

    @Override
    public PaasPage<LoginUser> getLoginUserPage(Pageable pageable, String username, String statusCd) {
        PageUtil.setPageAndOrderBy(pageable);
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(username);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        loginUser.setStatusCd(statusCd);
        Page<LoginUser> loginUserPage = loginUserRepository.getLoginUserPage(loginUser);
        return PageUtil.transformPage(loginUserPage);
    }

    @Override
    public LoginUser saveLoginUser(LoginUser loginUser) {
        if(null == loginUser.getLoginUserId()){
            loginUser.setCreateTime(new Date());
            loginUser.setStatusCd(CommonConstant.ADD_STATUS_CD);
            loginUserRepository.saveLoginUser(loginUser);
        }else{
            loginUserRepository.updateLoginUser(loginUser);
            return loginUser;
        }

        return loginUser;
    }

    @Override
    public LoginUser getLoginUserBasicInfo(Long loginUserId) {
        return loginUserRepository.getLoginUserBasicInfo(loginUserId);
    }

    @Override
    public void delLoginUser(Long loginUserId) {
        loginUserRepository.delLoginUser(loginUserId);
    }

    @Override
    public LoginUser updateLoginUser(Long loginUserId, LoginUser loginUser) {
        loginUserRepository.updateLoginUser(loginUser);
        return loginUser;
    }
}
